﻿Class MainWindow
    Dim dKarten As New Dictionary(Of Int16, String)
    Dim dPoints As New Dictionary(Of String, Int16)
    Dim same As New Dictionary(Of Int16, Int16)
    Dim _RnDKarte As New Random
    Dim _luck As Integer
    Dim _Punkte As Integer = 0
    Dim _cpPunkte As Integer = 0
    Dim Verlorenen As Integer = 0
    Dim Gewonnenen As Integer = 0
    Dim clicks As Integer = 1
    Dim cpclicks As Integer = 1

    Dim cpKarte01 As New Karte
    Dim cpKarte02 As New Karte
    Dim cpKarte03 As New Karte
    Dim cpKarte04 As New Karte
    Dim cpKarte05 As New Karte

    'Karten und Punkte Dictionary
    Public Sub iniDictionarys()
        dKarten.Add(1, "pics/clubs-2-150.png")
        dKarten.Add(2, "pics/clubs-3-150.png")
        dKarten.Add(3, "pics/clubs-4-150.png")
        dKarten.Add(4, "pics/clubs-5-150.png")
        dKarten.Add(5, "pics/clubs-6-150.png")
        dKarten.Add(6, "pics/clubs-7-150.png")
        dKarten.Add(7, "pics/clubs-8-150.png")
        dKarten.Add(8, "pics/clubs-9-150.png")
        dKarten.Add(9, "pics/clubs-10-150.png")
        dKarten.Add(10, "pics/clubs-j-150.png")
        dKarten.Add(11, "pics/clubs-q-150.png")
        dKarten.Add(12, "pics/clubs-k-150.png")
        dKarten.Add(13, "pics/clubs-a-150.png")
        dKarten.Add(14, "pics/diamonds-2-150.png")
        dKarten.Add(15, "pics/diamonds-3-150.png")
        dKarten.Add(16, "pics/diamonds-4-150.png")
        dKarten.Add(17, "pics/diamonds-5-150.png")
        dKarten.Add(18, "pics/diamonds-6-150.png")
        dKarten.Add(19, "pics/diamonds-7-150.png")
        dKarten.Add(20, "pics/diamonds-8-150.png")
        dKarten.Add(21, "pics/diamonds-9-150.png")
        dKarten.Add(22, "pics/diamonds-10-150.png")
        dKarten.Add(23, "pics/diamonds-j-150.png")
        dKarten.Add(24, "pics/diamonds-q-150.png")
        dKarten.Add(25, "pics/diamonds-k-150.png")
        dKarten.Add(26, "pics/diamonds-a-150.png")
        dKarten.Add(27, "pics/hearts-2-150.png")
        dKarten.Add(28, "pics/hearts-3-150.png")
        dKarten.Add(29, "pics/hearts-4-150.png")
        dKarten.Add(30, "pics/hearts-5-150.png")
        dKarten.Add(31, "pics/hearts-6-150.png")
        dKarten.Add(32, "pics/hearts-7-150.png")
        dKarten.Add(33, "pics/hearts-8-150.png")
        dKarten.Add(34, "pics/hearts-9-150.png")
        dKarten.Add(35, "pics/hearts-10-150.png")
        dKarten.Add(36, "pics/hearts-j-150.png")
        dKarten.Add(37, "pics/hearts-q-150.png")
        dKarten.Add(38, "pics/hearts-k-150.png")
        dKarten.Add(39, "pics/hearts-a-150.png")
        dKarten.Add(40, "pics/spades-2-150.png")
        dKarten.Add(41, "pics/spades-3-150.png")
        dKarten.Add(42, "pics/spades-4-150.png")
        dKarten.Add(43, "pics/spades-5-150.png")
        dKarten.Add(44, "pics/spades-6-150.png")
        dKarten.Add(45, "pics/spades-7-150.png")
        dKarten.Add(46, "pics/spades-8-150.png")
        dKarten.Add(47, "pics/spades-9-150.png")
        dKarten.Add(48, "pics/spades-10-150.png")
        dKarten.Add(49, "pics/spades-j-150.png")
        dKarten.Add(50, "pics/spades-q-150.png")
        dKarten.Add(51, "pics/spades-k-150.png")
        dKarten.Add(52, "pics/spades-a-150.png")

        dPoints.Add("a", 11)
        dPoints.Add("k", 10)
        dPoints.Add("q", 10)
        dPoints.Add("j", 10)
        dPoints.Add("10", 10)
        dPoints.Add("9", 9)
        dPoints.Add("8", 8)
        dPoints.Add("7", 7)
        dPoints.Add("6", 6)
        dPoints.Add("5", 5)
        dPoints.Add("4", 4)
        dPoints.Add("3", 3)
        dPoints.Add("2", 2)
    End Sub
    'Mainload
    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        karte1.Source = Nothing
        karte2.Source = Nothing
        karte3.Source = Nothing
        karte4.Source = Nothing
        karte5.Source = Nothing

        karte1.Margin = New Thickness(0, 0, 0, 0)
        karte2.Margin = New Thickness(0, 0, 0, 0)
        karte3.Margin = New Thickness(0, 0, 0, 0)
        karte4.Margin = New Thickness(0, 0, 0, 0)
        karte5.Margin = New Thickness(0, 0, 0, 0)

        cpkarte1.Margin = New Thickness(0, 0, 0, 0)
        cpkarte2.Margin = New Thickness(0, 0, 0, 0)
        cpkarte3.Margin = New Thickness(0, 0, 0, 0)
        cpkarte4.Margin = New Thickness(0, 0, 0, 0)
        cpkarte5.Margin = New Thickness(0, 0, 0, 0)
        cpkarte1.Source = Nothing
        cpkarte2.Source = Nothing
        cpkarte3.Source = Nothing
        cpkarte4.Source = Nothing
        cpkarte5.Source = Nothing

        iniDictionarys()
    End Sub
    'Filtert aus der Karte die anzahl der Punkte raus die im Dic stehen.
    Public Function pointCutter(ByVal Card As String)
        Dim picPoint As String = Card
        picPoint = picPoint.Replace("pics/", "")
        picPoint = picPoint.Replace("-150.png", "")
        picPoint = picPoint.Replace("diamonds-", "")
        picPoint = picPoint.Replace("hearts-", "")
        picPoint = picPoint.Replace("spades-", "")
        picPoint = picPoint.Replace("clubs-", "")
        Return picPoint
    End Function
    'Erstellt eine Zufallszahl und Checkt ob die Karte schon im Spiel ist
    Public Sub lucky()
        _luck = _RnDKarte.Next(1, 52)
        If Not same.ContainsKey(_luck) Then
            same.Add(_luck, 1)
        Else
            lucky()
        End If
    End Sub
    'Neue Karte!!
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Button3.IsEnabled = True
        lucky()
        Dim picPoint As String = pointCutter(dKarten.Item(_luck))
        _Punkte = _Punkte + dPoints.Item(picPoint)
        'Player
        Select Case cpclicks
            Case 1
                Dim Karte01 As New Karte
                Karte01.ID = 1
                Karte01.Wert = 2
                Karte01.pic = dKarten.Item(_luck)
                karte1.Source = New BitmapImage(New Uri(Karte01.pic, UriKind.Relative))
                cpclicks = cpclicks + 1
            Case 2
                Dim Karte02 As New Karte
                Karte02.ID = 2
                Karte02.Wert = 2
                Karte02.pic = dKarten.Item(_luck)
                karte2.Source = New BitmapImage(New Uri(Karte02.pic, UriKind.Relative))
                cpclicks = cpclicks + 1
            Case 3
                Dim Karte03 As New Karte
                Karte03.ID = 3
                Karte03.Wert = 2
                Karte03.pic = dKarten.Item(_luck)
                karte3.Source = New BitmapImage(New Uri(Karte03.pic, UriKind.Relative))
                cpclicks = cpclicks + 1
            Case 4
                Dim Karte04 As New Karte
                Karte04.ID = 4
                Karte04.Wert = 2
                Karte04.pic = dKarten.Item(_luck)
                karte4.Source = New BitmapImage(New Uri(Karte04.pic, UriKind.Relative))
                cpclicks = cpclicks + 1
            Case 5
                Dim Karte05 As New Karte
                Karte05.ID = 5
                Karte05.Wert = 2
                Karte05.pic = dKarten.Item(_luck)
                karte5.Source = New BitmapImage(New Uri(Karte05.pic, UriKind.Relative))
                cpclicks = cpclicks + 1
            Case Else
                MsgBox("Maximale Anzahl an Karten erreicht!")
        End Select
        CPU()

        tbPunkte.Text = _Punkte.ToString
        If _Punkte > 21 Then
            Button3_Click(Me, e)
        End If
    End Sub
    'CPU Holt sich eine Karte
    Private Sub CPU()
        lucky()
        Dim picPoint As String = pointCutter(dKarten.Item(_luck))

        If _cpPunkte >= 21 Then
            Exit Sub
        End If

        _cpPunkte = _cpPunkte + dPoints.Item(picPoint)
        Select Case clicks
            Case 1
                cpKarte01.ID = 1
                cpKarte01.Wert = 2
                cpKarte01.pic = dKarten.Item(_luck)
                cpkarte1.Source = New BitmapImage(New Uri("pics/back-blue-150-1.png", UriKind.Relative))
                clicks = clicks + 1
            Case 2

                cpKarte02.ID = 2
                cpKarte02.Wert = 2
                cpKarte02.pic = dKarten.Item(_luck)
                cpkarte2.Source = New BitmapImage(New Uri("pics/back-blue-150-1.png", UriKind.Relative))
                clicks = clicks + 1
            Case 3

                cpKarte03.ID = 3
                cpKarte03.Wert = 2
                cpKarte03.pic = dKarten.Item(_luck)
                cpkarte3.Source = New BitmapImage(New Uri("pics/back-blue-150-1.png", UriKind.Relative))
                clicks = clicks + 1
            Case 4

                cpKarte04.ID = 4
                cpKarte04.Wert = 2
                cpKarte04.pic = dKarten.Item(_luck)
                cpkarte4.Source = New BitmapImage(New Uri("pics/back-blue-150-1.png", UriKind.Relative))
                clicks = clicks + 1
            Case 5

                cpKarte05.ID = 5
                cpKarte05.Wert = 2
                cpKarte05.pic = dKarten.Item(_luck)
                cpkarte5.Source = New BitmapImage(New Uri("pics/back-blue-150-1.png", UriKind.Relative))
                clicks = clicks + 1
        End Select
        'If _Punkte > 21 Then
        '    MsgBox("You WIN!!")
        'End If
        'If _Punkte = 21 Then
        '    MsgBox("You Looosee!!")
        'End If
    End Sub
    'Neues Spiel!! Alles auf 0 setzten!
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        same.Clear()
        Button1.IsEnabled = True
        Button3.IsEnabled = True
        clicks = 1
        cpclicks = 1
        _Punkte = 0
        _cpPunkte = 0
        tbPunkte.Text = "0"
        karte1.Source = Nothing
        karte2.Source = Nothing
        karte3.Source = Nothing
        karte4.Source = Nothing
        karte5.Source = Nothing
        karte1.Margin = New Thickness(0, 0, 0, 0)
        karte2.Margin = New Thickness(0, 0, 0, 0)
        karte3.Margin = New Thickness(0, 0, 0, 0)
        karte4.Margin = New Thickness(0, 0, 0, 0)
        karte5.Margin = New Thickness(0, 0, 0, 0)

        'Gewonnen und verloren zurücksetzten!
        Gewonnenen = 0
        Verlorenen = 0
        wins.Text = Gewonnenen
        lose.Text = Verlorenen


        cpkarte1.Margin = New Thickness(0, 0, 0, 0)
        cpkarte2.Margin = New Thickness(0, 0, 0, 0)
        cpkarte3.Margin = New Thickness(0, 0, 0, 0)
        cpkarte4.Margin = New Thickness(0, 0, 0, 0)
        cpkarte5.Margin = New Thickness(0, 0, 0, 0)
        cpkarte1.Source = Nothing
        cpkarte2.Source = Nothing
        cpkarte3.Source = Nothing
        cpkarte4.Source = Nothing
        cpkarte5.Source = Nothing
    End Sub
    'Spiel abschließen und CPU holt sich restliche karten
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click
        If _cpPunkte <= _RnDKarte.Next(16, 18) Then
            CPU()
            Button3_Click(Me, e)
        Else

            If clicks = 2 Then
                cpkarte1.Source = New BitmapImage(New Uri(cpKarte01.pic, UriKind.Relative))
            End If
            If clicks = 3 Then
                cpkarte1.Source = New BitmapImage(New Uri(cpKarte01.pic, UriKind.Relative))
                cpkarte2.Source = New BitmapImage(New Uri(cpKarte02.pic, UriKind.Relative))
            End If
            If clicks = 4 Then
                cpkarte1.Source = New BitmapImage(New Uri(cpKarte01.pic, UriKind.Relative))
                cpkarte2.Source = New BitmapImage(New Uri(cpKarte02.pic, UriKind.Relative))
                cpkarte3.Source = New BitmapImage(New Uri(cpKarte03.pic, UriKind.Relative))
            End If
            If clicks = 5 Then
                cpkarte1.Source = New BitmapImage(New Uri(cpKarte01.pic, UriKind.Relative))
                cpkarte2.Source = New BitmapImage(New Uri(cpKarte02.pic, UriKind.Relative))
                cpkarte3.Source = New BitmapImage(New Uri(cpKarte03.pic, UriKind.Relative))
                cpkarte4.Source = New BitmapImage(New Uri(cpKarte04.pic, UriKind.Relative))
            End If
            If clicks = 6 Then
                cpkarte1.Source = New BitmapImage(New Uri(cpKarte01.pic, UriKind.Relative))
                cpkarte2.Source = New BitmapImage(New Uri(cpKarte02.pic, UriKind.Relative))
                cpkarte3.Source = New BitmapImage(New Uri(cpKarte03.pic, UriKind.Relative))
                cpkarte4.Source = New BitmapImage(New Uri(cpKarte04.pic, UriKind.Relative))
                cpkarte5.Source = New BitmapImage(New Uri(cpKarte05.pic, UriKind.Relative))
            End If
            Brain()
            '  Round_End()
        End If

    End Sub
    'Setzt alles wierder auf null um von neu zu starten
    Public Function Round_End()
        same.Clear()
        Button1.IsEnabled = True
        Button3.IsEnabled = True
        clicks = 1
        cpclicks = 1

        tbPunkte.Text = "0"
        karte1.Source = Nothing
        karte2.Source = Nothing
        karte3.Source = Nothing
        karte4.Source = Nothing
        karte5.Source = Nothing
        karte1.Margin = New Thickness(0, 0, 0, 0)
        karte2.Margin = New Thickness(0, 0, 0, 0)
        karte3.Margin = New Thickness(0, 0, 0, 0)
        karte4.Margin = New Thickness(0, 0, 0, 0)
        karte5.Margin = New Thickness(0, 0, 0, 0)

        cpkarte1.Margin = New Thickness(0, 0, 0, 0)
        cpkarte2.Margin = New Thickness(0, 0, 0, 0)
        cpkarte3.Margin = New Thickness(0, 0, 0, 0)
        cpkarte4.Margin = New Thickness(0, 0, 0, 0)
        cpkarte5.Margin = New Thickness(0, 0, 0, 0)
        cpkarte1.Source = Nothing
        cpkarte2.Source = Nothing
        cpkarte3.Source = Nothing
        cpkarte4.Source = Nothing
        cpkarte5.Source = Nothing

        _Punkte = 0
        _cpPunkte = 0
        Return Nothing

    End Function
    'Unentschieden
    Public Sub Unentschieden()
        MsgBox(String.Concat("Unentschieden!!", vbCr, "PC: ", _cpPunkte.ToString, vbCr, "Ich: ", _Punkte.ToString))
        Round_End()
    End Sub
    'Hier wird entschieden wer gewinnt
    Public Sub Brain()

        If _cpPunkte > 0 AndAlso _Punkte > 0 Then
            If _cpPunkte = _Punkte Then
                Unentschieden()
                Round_End()
                Exit Sub
            End If

        End If
        'Computer Verlier

        If _cpPunkte >= 21 AndAlso _Punkte >= 21 Then
            If _cpPunkte - 21 > _Punkte - 21 Then
                win()
                Exit Sub
            End If
            If _cpPunkte - 21 = _Punkte - 21 Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte <= 21 AndAlso _Punkte <= 21 Then
            If 21 - _cpPunkte > 21 - _Punkte Then
                win()
                Exit Sub
            End If
            If 21 - _cpPunkte = 21 - _Punkte Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte <= 21 AndAlso _Punkte >= 21 Then
            If 21 - _cpPunkte > _Punkte - 21 Then
                win()
                Exit Sub
            End If
            If 21 - _cpPunkte = _Punkte - 21 Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte >= 21 AndAlso _Punkte <= 21 Then
            If _cpPunkte - 21 > 21 - _Punkte Then
                win()
                Exit Sub
            End If
            If _cpPunkte - 21 = 21 - _Punkte Then
                Unentschieden()
                Exit Sub
            End If
        End If

        'Computer Gewinnt
        If _cpPunkte >= 21 AndAlso _Punkte >= 21 Then
            If _cpPunkte - 21 < _Punkte - 21 Then
                Verloren()
                Exit Sub
            End If
            If _cpPunkte - 21 = _Punkte - 21 Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte <= 21 AndAlso _Punkte <= 21 Then
            If 21 - _cpPunkte < 21 - _Punkte Then
                Verloren()
                Exit Sub
            End If
            If 21 - _cpPunkte = 21 - _Punkte Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte <= 21 AndAlso _Punkte >= 21 Then
            If 21 - _cpPunkte < _Punkte - 21 Then
                Verloren()
                Exit Sub
            End If
            If 21 - _cpPunkte = _Punkte - 21 Then
                Unentschieden()
                Exit Sub
            End If
        End If
        If _cpPunkte >= 21 AndAlso _Punkte <= 21 Then
            If _cpPunkte - 21 < 21 - _Punkte Then
                Verloren()
                Exit Sub
            End If
            If _cpPunkte - 21 = 21 - _Punkte Then
                Unentschieden()
                Exit Sub
            End If
        End If
    End Sub
    'Passiert wenn der Spieler gewinnt
    Public Sub win()
        Gewonnenen = Gewonnenen + 1
        wins.Text = Gewonnenen
        MsgBox("Gewonnen!")
        Round_End()
    End Sub
    'Passiert wenn der Spieler verliert
    Public Sub Verloren()
        Button1.IsEnabled = False
        Button3.IsEnabled = False
        MsgBox("Verloren!")
        Verlorenen = Verlorenen + 1
        lose.Text = Verlorenen
        Round_End()

    End Sub
End Class